#ifndef CAN_H
#define CAN_H

/**
  ******************************************************************************
  * @file    can.h
  * @author  Dajin Li
  * @version V0.0.1
  * @date    08-08-2015 
  * @brief   This file contains CANopen specific functions.
  *          
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; KION(SZ) R&D Center
  *
  * Licensed under KION, (the "License");
  *
  ******************************************************************************
  */



/*****************************************************************************************
Macros
******************************************************************************************/
#define FILTER_NUMBER_DEF                      14

#define CANOPEN_FILTER_NUMBER_0                0
#define CANOPEN_FILTER_NUMBER_1                1
#define CANOPEN_FILTER_NUMBER_2                2
#define CANOPEN_FILTER_NUMBER_3                3
#define CANOPEN_FILTER_NUMBER_4                4
#define CANOPEN_FILTER_NUMBER_5                5
#define CANOPEN_FILTER_NUMBER_6                6
#define CANOPEN_FILTER_NUMBER_7                7
#define CANOPEN_FILTER_NUMBER_8                8
#define CANOPEN_FILTER_NUMBER_9                9
#define CANOPEN_FILTER_NUMBER_10               10
#define CANOPEN_FILTER_NUMBER_11               11
#define CANOPEN_FILTER_NUMBER_12               12
#define CANOPEN_FILTER_NUMBER_13               13

#define FLASHING_DELAY_1                       150000
#define FLASHING_DELAY_2                       300000

#define FLASHING_DELAY_3                       15
#define FLASHING_DELAY_4                       30


#define CAN_MASTER_ID_DEF                      0  //default master id

#define CAN_SLAVE_ID_DEF                       4 //default slave id 

#define GPIO_CANOPEN                           GPIOA  
#define GPIO_Pin_CANOPEN_RX                    GPIO_Pin_11
#define GPIO_Pin_CANOPEN_TX                    GPIO_Pin_12



#define NMT                         0x0     // Network Management
#define SYNC_ERR                    0x1     // Both Sync (COB-ID=0) & Error (COB-ID=Error)
#define RESV_1                      0x2
#define PDO_MISO                    0x3     // Process Data Object (Master In  Slave Out)
#define PDO_MOSI                    0x4     // Process Data Object (Master Out Slave In)
#define PDO_SYS1                    0x5     // Process Data Object (System Broadcast 1)
#define PDO_SYS2                    0x6     // Process Data Object (System Broadcast 2)
#define PDO_SYS3                    0x7     // Process Data Object (System Broadcast 3)
#define PDO_SYS4                    0x8     // Process Data Object (System Broadcast 4)
#define IDO_MISO                    0x9     // Index Data Object (Master In  Slave Out)
#define IDO_MOSI                    0xA     // Index Data Object (Master Out Slave In)
#define SDO_MISO                    0xB     // Service Data Object (Master In  Slave Out)
#define SDO_MOSI                    0xC     // Service Data Object (Master Out Slave In)
#define RESV_2                      0xD
#define NODE                        0xE     // Node Guard Message
#define RESV_3                      0xF

/* CAN open PDO id definitions */

#define PDO1_MISO		            0x3     // Process Data Object 1 (Master In  Slave Out)
#define PDO1_MOSI		            0x4     // Process Data Object 1 (Master Out Slave In)
#define PDO2_MISO		            0x5     // Process Data Object 2 (Master In  Slave Out)
#define PDO2_MOSI		            0x6     // Process Data Object 2 (Master Out Slave In)
#define PDO3_MISO		            0x7     // Process Data Object 3 (Master In  Slave Out)
#define PDO3_MOSI		            0x8     // Process Data Object 3 (Master Out Slave In)
#define PDO4_MISO		            0x9     // Process Data Object 4 (Master In  Slave Out)
#define PDO4_MOSI		            0xA     // Process Data Object 4 (Master Out Slave In)
#define HEARTBEAT		            0xE     // Heartbeat
#define CAN_DATA_LEN_DEF            8


#define C_RCV                       0                 // Receive Bit
#define C_XMT                       1                 // Transmit Bit

#define MAX_MO                      32      // Number of Message Objects (buffers)
#define YES                         1
#define NO                          0


#define C_SYNC                      0x1     // Send on Sync message
#define C_CYCLIC                    0x2     // . "  "  clock tick
#define C_EVENT                     0x3     // . "  "  event

#define MOS_NO_ID                   0x01    //  No ID, No Definition
#define MOS_HAS_ID                  0x02    //  Has ID, No Definition
#define MOS_INACTIVE                0x04    //  Has ID, Has Definition, Not running
#define MOS_ACTIVE                  0x08    //  Has ID, Has Definition, Actively running

#define CANOPEN_RX_MSG_BUF          128
#define CANOPEN_CAN                 CAN1
#define CANOPEN_CAN_FIFO            CAN_FIFO0
#define CANOPEN_CAN_RX_IRQ          USB_LP_CAN1_RX0_IRQn

#define CANOPEN_FAULT_DELAY         100


enum can_baud_rates  
{
    CAN_100KBAUD = -1,  // -1
    CAN_125KBAUD,       //  0
    CAN_250KBAUD,       //  1
    CAN_500KBAUD,       //  2
    CAN_BAD_BAUD 
};
/*****************************************************************************************
Global variables
******************************************************************************************/
extern u16 can_node_id;                     
extern s16 can_baud_rate;
extern s16 can_bus_on;                      //  617 -  120 source\can_driver.c
extern s16 can_error_status;                //  618 -  121
extern s16 working_can_slave_id;            //  868 -  310 source\can_driver.c
extern s16 working_can_master_id;           //  869 -   43 source\can_open.c
extern s16 can_pdo_timeout;
extern s16 can_pdo_timeout_period;
extern CanRxMsg CANopenRxMsgBuf[];
extern u8 CANopen_rx_buf_head;
extern u8 CANopen_rx_buf_tail;
/*****************************************************************************************
Global functions
******************************************************************************************/
extern void  CAN_Config(void);
extern u16 make_cob_id(u16 type, u16 master_id, u16 slave_id);
extern u8 CAN_Set_Baudrate(u8 rate);
void CAN_Enable(void);
void CAN_Disable(void);
void KWP_send_pending_msg(void);

#endif

